package com.lsmw.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsmw.admin.entity.LsAdmin;
import com.lsmw.admin.mapper.LsAdminMapper;
import com.lsmw.admin.service.LsAdminService;
import com.lsmw.commonutils.R;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 三号男嘉宾
 * @since 2021-03-23
 */
@Service
public class LsAdminServiceImpl extends ServiceImpl<LsAdminMapper, LsAdmin> implements LsAdminService {

    /**
     * @return 所有管理员信息
     */
    @Override
    public R getAdmins() {
        List<LsAdmin> list = this.list();
        return list.size() != 0 ? R.ok().data("admins",list) : R.ok().data("msg","目前没有admin哦");
    }

    /**
     * @param adminId 管理员id
     * @return 对应id的管理员信息
     */
    @Override
    public R getAdminById(String adminId) {
        LsAdmin byId = this.getById(adminId);
        byId.setAdminPwd("");
        return byId != null
                ?
                R.ok().data("row",byId)
                :
                R.ok().data("msg","没有找到id为"+adminId+"的管理员");
    }

    /**
     * 添加管理员
     *
     * @param lsAdmin
     * @return
     */
    @Override
    public R addAdmin(LsAdmin lsAdmin) {
        Map<String,String> adminInfo = new HashMap<>();
        if(!this.list(new LambdaQueryWrapper<LsAdmin>().eq(LsAdmin::getAdminName,lsAdmin.getAdminName())).isEmpty()){
            return R.invalidRequest().data("msg","用户名重复,请重新输入!");
        }
        adminInfo.put("adminName",lsAdmin.getAdminName());
        adminInfo.put("adminPwd",lsAdmin.getAdminPwd());
        return this.save(lsAdmin)?R.created().data("admin",adminInfo):R.invalidRequest().data("msg","请检查信息或联系管理员");
    }

    /**
     * 根据id删除管理员
     *
     * @param adminId
     * @return
     */
    @Override
    public R deleteAdminById(String adminId) {
        LsAdmin byId = this.getById(adminId);
        if(byId != null && this.removeById(adminId)){
            return R.ok().data("admin",byId);
        }
        return R.ok().data("msg","请检查id是否正确或联系管理员");
    }

    /**
     * 根据多个id删除管理员
     *
     * @param adminIds
     * @return
     */
    @Override
    public R deleteAdminByIds(List<String> adminIds) {
        List<LsAdmin> lsAdmins = this.listByIds(adminIds);
        if(lsAdmins.size() == adminIds.size() && this.removeByIds(adminIds)){
            return R.ok().data("admins",lsAdmins);
        }
        return R.ok().data("msg","请检查id是否正确或联系管理员");
    }
}
